package com.banciyuan.bcywebview.biz.detail.complex.holder;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2016/8/15.
 */
public class FloatBottom implements View.OnClickListener {
    private Context mContext;
    private ImageView recommend_iv;
    private TextView recommend_tv;
    private TextView comment_tv;
    private TextView praise_tv;
    private ImageView praise_iv;
    private ImageView ivLightSwitch;

    private boolean isPraiseRunning;


    public FloatBottom(View rootview, Context context) {
        mContext = context;
        recommend_iv = (ImageView) rootview.findViewById(R.id.iv_detail_recommend);
        recommend_tv = (TextView) rootview.findViewById(R.id.tv_detail_recommend);
        comment_tv = (TextView) rootview.findViewById(R.id.tv_detail_comment);
        praise_tv = (TextView) rootview.findViewById(R.id.tv_detail_praise);
        praise_iv = (ImageView) rootview.findViewById(R.id.iv_detail_praise);
        ivLightSwitch = (ImageView) rootview.findViewById(R.id.iv_light_switch);
        rootview.findViewById(R.id.view_comment_action).setOnClickListener(this);
        rootview.findViewById(R.id.view_detail_recommend).setOnClickListener(this);
        rootview.findViewById(R.id.view_detail_praise).setOnClickListener(this);
        rootview.findViewById(R.id.view_detail_comment).setOnClickListener(this);
    }

    public void processBottom(Complex complex) {
        updateComment(complex);
        updateDingWithOutAnimation(complex);
        updateTuijian(complex);
        setNight(complex);
    }

    private void setNight(Complex complex) {
        if (complex.getTimelineType() == TimelineFactory.TIMELINE_TYPE_WRITER) {
            ivLightSwitch.setVisibility(View.VISIBLE);
            ivLightSwitch.setOnClickListener(this);
            lightStatus();
        }
    }

    public void updateTuijian(Complex complex) {
        if (complex.isHave_tuijian()) {
            recommend_iv.setImageResource(R.drawable.detail_recommend_done);
            recommend_tv.setText(mContext.getString(R.string.all_recommended));
        } else {
            recommend_iv.setImageResource(R.drawable.detail_recommend);
            recommend_tv.setText(mContext.getString(R.string.all_recommend));
        }
    }

    public void updateDingWithOutAnimation(Complex complex) {
        if (complex.isHave_ding()) {
            praise_iv.setImageResource(R.drawable.detail_praise_done);
            praise_tv.setText(String.valueOf(complex.getDing_num()));
        } else {
            praise_iv.setImageResource(R.drawable.detail_praise);
            if (complex.getDing_num() == 0) {
                praise_tv.setText(mContext.getString(R.string.good));
            } else {
                praise_tv.setText(String.valueOf(complex.getDing_num()));
            }
        }
    }

    public void updateDing(final Complex complex) {
        if (complex.isHave_ding()) {
            isPraiseRunning = true;
            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(UIUtils.dip2px(24, mContext), UIUtils.dip2px(24, mContext));
            praise_iv.setLayoutParams(ll);
            praise_iv.setScaleX(2f);
            praise_iv.setScaleY(2f);

            praise_iv.setImageResource(R.drawable.frame_praise);
            AnimationDrawable drawable = (AnimationDrawable) praise_iv.getDrawable();
            drawable.start();
            praise_iv.postDelayed(new Runnable() {
                @Override
                public void run() {
                    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    praise_iv.setLayoutParams(ll);
                    praise_iv.setScaleX(1f);
                    praise_iv.setScaleY(1f);

                    praise_iv.setImageResource(R.drawable.detail_praise_done);
                    praise_tv.setText(String.valueOf(complex.getDing_num()));
                    isPraiseRunning = false;
                }
            }, 24 * 20);
        } else {
            praise_iv.setImageResource(R.drawable.detail_praise);
            if (complex.getDing_num() == 0) {
                praise_tv.setText(mContext.getString(R.string.good));
            } else {
                praise_tv.setText(String.valueOf(complex.getDing_num()));
            }
        }
    }

    public void updateComment(Complex complex) {
        int commentNum = complex.getReply_count();
        if (commentNum == 0) {
            comment_tv.setText(mContext.getString(R.string.comment));
        } else {
            comment_tv.setText(String.valueOf(commentNum));
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_detail_recommend:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.RECOMMEND_EVENT);
                break;
            case R.id.view_detail_praise:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.LIKE_EVENT);
                break;
            case R.id.view_detail_comment:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.COMMENT_EVENT, 1);
                break;
            case R.id.view_comment_action:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.COMMENT_EVENT, 0);
                break;
            case R.id.iv_light_switch:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.LIGHT, ivLightSwitch.isSelected());
                break;
        }
    }

    public boolean isPraiseRunning() {
        return isPraiseRunning;
    }

    private void lightStatus() {
        boolean is_light = SPHelper.getBoolean(mContext, SPConstant.SPNAME_CLOSELIGHT, SPConstant.NOVEL_LIGHT, true);
        if (is_light) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                ivLightSwitch.setImageDrawable(mContext.getResources().getDrawable(R.drawable.light_off, mContext.getTheme()));
            } else {
                ivLightSwitch.setImageDrawable(mContext.getResources().getDrawable(R.drawable.light_off));
            }
        } else {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                ivLightSwitch.setImageDrawable(mContext.getResources().getDrawable(R.drawable.light_on, mContext.getTheme()));
            } else {
                ivLightSwitch.setImageDrawable(mContext.getResources().getDrawable(R.drawable.light_on));
            }
        }
        ivLightSwitch.setSelected(is_light);
    }
}
